<template>
  <div>
    <el-form
      :model="facilityData"
      ref="form"
      label-width="80px"
      :inline="false"
      size="default"
      :props="{
        label: (data) => {
          console.log('data', data)
          // return data.meta.title
        }
      }"
    >
      <el-form-item label="设备编码">
        <el-input v-model="facilityData.num" />
      </el-form-item>
      <el-form-item label="防火设备名称">
        <el-input v-model="facilityData.name" />
      </el-form-item>
      <el-form-item label="区域信息">
        <el-input v-model="facilityData.parkName" />
      </el-form-item>
      <el-form-item label="安装位置" prop="address">
        <div class="flex flex-1">
          <el-input v-model="facilityData.position" readonly placeholder="请点击按钮进行地图选址" />
          <el-button type="primary" size="default" @click="drawer = true">地图选址</el-button>
        </div>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="facilityData.remark" />
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="onSubmit" v-if="edata">确认修改</el-button>
        <el-button type="primary" @click="onSubmit" v-else>立即新增</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
    <el-drawer title="地图选址" v-model="drawer" direction="btt" size="60%">
      <GodMap v-model="facilityData.position" />
    </el-drawer>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { facilityDataType } from '../types'
import { fireFacilityPost, fireFacilityPut } from '@/api/FireWarning'
import { onMounted } from 'vue'
import GodMap from './god_map.vue'
import { usefireData } from '@/store/fire'
const emit = defineEmits()

const getLat = usefireData()
const facilityData: facilityDataType = reactive({
  num: '',
  name: '',
  parkName: '',
  position: '',
  remark: '',
  lng: 0,
  lat: 0
})

// 新增
const onSubmit = () => {
  // fireFacilityPost(facilityData).then((res) => {
  //     emit('update:table-data', {
  //         ...facilityData,
  //         objectId: res.data.objectId
  //     })
  // })

  if (props.edata) {
    // 修改

    facilityData.lng = getLat.latlon!.lng
    facilityData.lat = getLat.latlon!.lat
    let { objectId } = props.edata
    fireFacilityPut(objectId, facilityData).then(() => {
      emit('update:table-data', {
        ...facilityData,
        objectId
      })
    })
  } else {
    //新增
    facilityData.lng = getLat.latlon!.lng
    facilityData.lat = getLat.latlon!.lat
    fireFacilityPost(facilityData).then((res) => {
      emit('update:table-data', {
        ...facilityData,
        objectId: res.data.objectId
      })
    })
  }
}
// 地图选址弹窗
const drawer = ref<boolean>(false)
// props数据具备单向性
type PropsType = {
  edata: facilityDataType | null
}
const props = defineProps<PropsType>()
console.log('接收到了父级的props', props.edata)
onMounted(() => {
  if (props.edata) {
    //点击到了编辑按钮
    // console.log(props.edata, 99999);
    let { num, parkName, name, position, remark, lat, lng } = props.edata
    // console.log(1212,name);

    facilityData.name = name
    facilityData.position = position
    facilityData.parkName = parkName
    facilityData.remark = remark
    facilityData.num = num
    facilityData.lat = lat
    facilityData.lng = lng

    // 修改
    // parkHealthPut(props.edata.objectId, roleDate)
  } else {
    //点击到了新增按钮

    console.log('else')
    facilityData.name = ''
    facilityData.position = ''
    facilityData.parkName = ''
    facilityData.remark = ''
    facilityData.num = ''
    facilityData.lat = 0
    facilityData.lng = 0
  }
})
</script>

<style scoped></style>
